﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SinaWeiboPlugin;
using SinaWeiboPlugin.Properties;
using Slippor.Douban.Radio.Assist;

namespace SinaWeiboUserControl
{
    public partial class ConfigControl : UserControl
    {
        private Settings _settings = SinaWeiboConfig.SettingManager.Settings as Settings;
        List<RadioButton> _radioButtons;

        public event EventHandler Closed;

        private DialogResult _dialogResult = DialogResult.None;
        public DialogResult DialogResult
        {
            get { return _dialogResult; }
        }

        public ConfigControl()
        {
            InitializeComponent();
            _radioButtons = new List<RadioButton> { radioButtonSong, radioButtonArtistsSong, radioButtonSongArtists };
            InitializeConfigs();
            HotKey.InitHotKeyTextBox(textBoxShortCut);
        }

        private void InitializeConfigs()
        {
            bool normal = false;
            foreach (RadioButton rb in _radioButtons)
            {
                if (rb.Tag.ToString() == _settings.DefaultString)
                {
                    normal = true;
                    rb.Checked = true;
                    break;
                }
            }
            if (!normal)
            {
                radioButtonCustom.Checked = true;
                textBoxCustom.Text = _settings.DefaultString;
            }
            checkBoxAutoUpdate.Checked = _settings.AutoUpdate;
            checkBoxDirect.Checked = _settings.DirectSend;
            checkBoxAutoShare.Checked = _settings.AutoShareFav;
            checkBoxErrorAlert.Checked = _settings.ErrorAlert;
            textBoxUserName.Text = _settings.UserName;
            textBoxPassword.Text = _settings.Password;
            textBoxShortCut.Text = _settings.ShortCutKey;

            checkBoxEnableFortune.Checked = _settings.EnableFortune;
            checkBoxSendArtist.Checked = _settings.FortuneWithArtists;
            checkBoxRandomOrder.Checked = _settings.RandomOrder;
            checkBoxAutoSendFortune.Checked =  _settings.AutoSendFortune;
            checkBoxOnceDay.Checked = _settings.FortuneOnceDay;
            checkedListBoxFortune.Items.Clear();
            foreach (var item in FortuneTeller.Fortunes)
            {
                checkedListBoxFortune.Items.Add(item, FortuneTeller.AvailabelFortunes.Contains(item));
            }
        }

        private void ConfirmConfigs()
        {
            _settings.UserName = textBoxUserName.Text;
            _settings.Password = textBoxPassword.Text;
            _settings.ShortCutKey = textBoxShortCut.Text;
            if (radioButtonCustom.Checked)
            {
                _settings.DefaultString = textBoxCustom.Text;
            }
            else
            {
                foreach (RadioButton rb in _radioButtons)
                {
                    if (rb.Checked)
                    {
                        _settings.DefaultString = rb.Tag.ToString();
                        break;
                    }
                }
            }
            _settings.AutoUpdate = checkBoxAutoUpdate.Checked;
            _settings.DirectSend = checkBoxDirect.Checked;
            _settings.AutoShareFav = checkBoxAutoShare.Checked;
            _settings.ErrorAlert = checkBoxErrorAlert.Checked;

            _settings.EnableFortune = checkBoxEnableFortune.Checked;
            _settings.RandomOrder = checkBoxRandomOrder.Checked;
            _settings.FortuneWithArtists = checkBoxSendArtist.Checked;
            _settings.AutoSendFortune = checkBoxAutoSendFortune.Checked;
            _settings.FortuneOnceDay = checkBoxOnceDay.Checked;
            List<String> fortunesList = new List<string>();
            foreach (var item in checkedListBoxFortune.CheckedItems)
            {
                fortunesList.Add(item.ToString());
            }
            String fortunes = String.Join(",", fortunesList.ToArray());
            if (_settings.AvailableFortunes != fortunes)
            {
                _settings.AvailableFortunes = fortunes;
                FortuneTeller.RefreshAvailabelFortunes(_settings.AvailableFortunes);
            }

            _settings.Save();
        }

        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            ConfirmConfigs();
            _dialogResult = DialogResult.OK;
            if (Closed != null)
            {
                Closed(sender, e);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            _dialogResult = DialogResult.Cancel;
            if (Closed != null)
            {
                Closed(sender, e);
            }
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定重置？", "提示", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                _settings.Reset();
                _settings.Save();
                InitializeConfigs();
            }
        }

        private void radioButtonCustom_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCustom.Enabled = radioButtonCustom.Checked;
        }

        private void checkBoxEnableFortune_CheckStateChanged(object sender, EventArgs e)
        {
            checkBoxAutoSendFortune.Enabled = checkBoxOnceDay.Enabled = checkBoxSendArtist.Enabled = checkBoxRandomOrder.Enabled = checkedListBoxFortune.Enabled = checkBoxEnableFortune.Checked;
        }

        private void linkLabelHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RadioUtility.Browse("http://slippor.blogbus.com/logs/84473549.html");
        }

        public void ShowTabPage(int pageIndex)
        {
            tabControlConfig.SelectedIndex = pageIndex;
        }

    }
}
